package online.sixfish.doubao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import online.sixfish.common.utils.DateTimeUtils;
import online.sixfish.doubao.enumration.NoticeStatusEnum;
import online.sixfish.doubao.mapper.NoticeMapper;
import online.sixfish.doubao.model.entity.Notice;
import online.sixfish.doubao.service.NoticeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class NoticeServiceImpl implements NoticeService {
    @Resource
    NoticeMapper noticeMapper;
    @Override
    public List<Notice> getRecentNotice() {
        LambdaQueryWrapper<Notice> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Notice::getStatus, NoticeStatusEnum.SHOW.getId());
        wrapper.orderByDesc(Notice::getStartTime);
        wrapper.last("LIMIT 5");
        List<Notice> notices = noticeMapper.selectList(wrapper);
        return notices;
    }

    @Override
    public Notice getById(Integer id) {
        return noticeMapper.selectById(id);
    }

    @Override
    public void updateNoticeStatus2Show() {
        Date start = new Date();

        log.info("[更新公告状态任务]-->开始执行，当前时间:{}", DateTimeUtils.format(start));
        int count = 0;
        QueryWrapper<Notice> wrapper = new QueryWrapper<>();
        Date now = new Date();
        wrapper.le("start_time", now)
                .ge("end_time", now)
                .ne("status", NoticeStatusEnum.SHOW.getId())
                .ne("status", 0);
        List<Notice> noticeList = noticeMapper.selectList(wrapper);
        for (Notice notice : noticeList) {
            notice.setStatus(NoticeStatusEnum.SHOW.getId());
            noticeMapper.updateById(notice);
            count++;
            log.info("[更新公告状态任务]-->执行中，修改id:[{}]状态成功", notice.getId());
        }
        Date end = new Date();
        log.info("[更新公告状态任务]-->执行完成，修改数量：{},总时长：{}ms", count, end.getTime() - start.getTime());
    }

    @Override
    public void updateNoticeStatus2Expired() {
        Date start = new Date();

        log.info("[更新过期公告状态任务]-->开始执行，当前时间:{}", DateTimeUtils.format(start));
        int count = 0;
        QueryWrapper<Notice> wrapper = new QueryWrapper<>();
        Date now = new Date();
        wrapper.le("end_time", now)
                .ne("status", NoticeStatusEnum.EXPIRED.getId())
                .ne("status", 0);

        List<Notice> noticeList = noticeMapper.selectList(wrapper);
        for (Notice notice : noticeList) {
            notice.setStatus(NoticeStatusEnum.EXPIRED.getId());
            noticeMapper.updateById(notice);
            count++;
            log.info("[更新过期公告状态任务]-->执行中，修改id:[{}]状态成功", notice.getId());
        }
        Date end = new Date();
        log.info("[更新过期公告状态任务]-->执行完成，修改数量：{},总时长：{}ms", count, end.getTime() - start.getTime());
    }
}
